﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Common.Background
{
    /// <summary>
    /// 树型结构统一构建与展现
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DataTree<T> where T : ITree<T>
    {
        IEnumerable<T> _list;
        public DataTree(IEnumerable<T> list)
        {
            _list = list.OrderBy(i => i.Level);
        }

        #region 树的格式化返回

        public string CreateDataTree(string name, int selectValue)
        {
            return CreateDataTree(name, selectValue, null);
        }
        public string CreateDataTree(string name, int selectValue, string controller)
        {
            return CreateDataTree(name, selectValue, 1, controller);
        }

        public string CreateDataTree(string name, int[] selectValue, int treeID, string controller)
        {
            return CreateDataTree(name, selectValue, treeID, true, controller, false);
        }
        public string CreateDataTree(string name, int[] selectValue, string controller)
        {
            return CreateDataTree(name, selectValue, 1, true, controller, false);
        }

        public string CreateDataTree(string name, int selectValue, string controller, bool displayButton)
        {
            return CreateDataTree(name, new int[] { selectValue }, 1, false, controller, displayButton);
        }

        public string CreateDataTree(string name, int selectValue, int treeID, string controller)
        {
            return CreateDataTree(name, new int[] { selectValue }, treeID, controller);
        }

        public string CreateDataTree(string name, int[] selectValue, int treeID, bool radioButton, string controller)
        {
            return CreateDataTree(name, selectValue, treeID, radioButton, controller, false);
        }

        public string CreateDataTree(string name, int[] selectValue, int treeID, bool radioButton, string controller, bool displayButton)
        {
            StringBuilder html = new StringBuilder();
            html.Append(@"<script>
                              $(function () {
                                  $('input[type=checkbox]').live('click', function () {
                                      if ($(this).attr('checked') == 'checked')
                                          $(this).nextAll('ul').find(':checkbox').attr('checked', 'checked');
                                      else
                                          $(this).nextAll('ul').find(':checkbox').removeAttr('checked');
                                  });
                              });
                          </script>");
            html.Append("<div id='" + name + "_Tree'><ul style='list-style-type:none'>");
            this.GetTree(html, GetCompleteTree(treeID), name, selectValue, radioButton, controller, displayButton);
            html.Append("</ul></div>");
            //            html.Append(@"
            //        <script>
            //        $(function () {
            //        $('#" + controller + @"_Tree').find('.display_hide').toggle(function () {
            //            $(this).closest('li').find('ul').hide('slow');
            //            $(this).html('<span class=display_hide><a href=\""tree\"" style=\""padding:5px;font-size:16px;text-decoration:none\"" onclick=\""return false\"">●</a></span>');
            //        }, function () {
            //            $(this).closest('li').find('ul').show('slow');
            //            $(this).html('<span class=\""display_hide\""><a href=\""tree\"" style=\""padding:5px;font-size:16px;text-decoration:none\"" onclick=\""return false\"">○</a></span>');
            //        });
            //    });
            //</script>");
            return html.ToString();
        }

        private void GetTree(StringBuilder html, T entity, string name, int[] selectValue, string controller)
        {
            GetTree(html, entity, name, selectValue, true, controller, false);
        }
        /// <summary>
        /// 得到树形结构
        /// </summary>
        /// <param name="html"></param>
        /// <param name="webmanagemenu"></param>
        private void GetTree(StringBuilder html, T entity, string name, int[] selectValue, bool radioButton, string controller, bool displayButton)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentException("name参数不能为空");

            html.Append("<li id='" + name + "_node" + entity.ID + "' style='list-style-type:none'>");

            string check = string.Empty;
            if (selectValue.Contains(entity.ID))
                check = "checked='checked'";
            if (radioButton)
            {
                html.AppendFormat("<input type='radio' " + check + " id='{0}' name='{1}' Value='{2}' />"
                                   , name + entity.ID, name, entity.ID);
            }
            else
            {
                html.AppendFormat("<input type='checkbox' " + check + " id='{0}' name='{1}' Value='{2}' />"
                   , name + entity.ID, name, entity.ID);
            }
            html.AppendFormat("<label  for='{0}'>{1}</label>", name + entity.ID, entity.Name);
            if (displayButton)
            {

                html.AppendFormat(
              @"<a href='/{1}/Edit?id={0}'>编辑</a> 
                <a href='/{1}/Delete?id={0}'>删除</a> 
                <a href='/{1}/Create?id={0}'>新建</a>", entity.ID, controller);
            }

            if (entity.Sons != null && entity.Sons.Count() > 0)
            {
                html.Append("<ul style='margin:0px;list-style-type:none'>");
                foreach (var item in entity.Sons)
                {
                    this.GetTree(html, item, name, selectValue, radioButton, controller, displayButton);
                }
                html.Append("</ul>");
            }
            html.Append("</li>");
        }
        #endregion

        #region 树的装载
        private T GetCompleteTree(int treeID)
        {
            var root = _list.First(i => i.ID == treeID);
            GetSubTree(root);
            return root;
        }

        private void GetSubTree(T tree)
        {
            List<T> sons = _list.Where(i => i.ParentID == tree.ID).ToList();
            tree.Sons = sons;
            sons.ForEach(i =>
            {
                i.Father = tree;
                GetSubTree(i);
            });
        }
        #endregion
    }

}